//
//  YKSearchOnlineViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/8.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSearchOnlineViewController.h"
#import "YKClientListHeaderView.h"
#import "YKSearchDrugItem.h"
#import "YKSaleModel.h"
@interface YKSearchOnlineViewController ()<UISearchBarDelegate>
@property (nonatomic, strong)NSArray *filterredDataSource;
@end

@implementation YKSearchOnlineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.placeholder = @"通用名/助记码/生产厂家";
    searchBar.delegate = self;
    [self.view addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    
    
    YKClientListHeaderView *listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"通用名",@"药品规格",@"生产厂家"] weights:@[@1,@1,@1] spaceing:0 leading:3 trailing:3];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(searchBar.mas_bottom);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.top.equalTo(listHeaderView.mas_bottom);
    }];
    self.filterredDataSource = self.dataSource;
    [self updateViews];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    [self updateTableViewByText:searchText];
}
- (void)updateTableViewByText:(NSString *)name{
    if (name.length == 0) {
        self.filterredDataSource = self.dataSource;
    }
    else {
        self.filterredDataSource = [self.dataSource tf_filteredWithPredicateBlock:^BOOL(id  _Nonnull obj, NSInteger idx) {
            
            YKSaleModel *model = obj;
            if ([model.name containsString:name] || [model.QUICK_CODE containsString:[name uppercaseString]] || [model.CENTERPRISE containsString:name]) {
                return YES;
            }
            return NO;
        }];
    }
    [self updateViews];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    NSString *name = searchBar.text;
    [self updateTableViewByText:name];
}

- (void)updateViews {
    [self.tableViewManager removeAllSections];
    TFTableViewSection *section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    
    
    
    for (YKSaleModel *model in self.filterredDataSource) {
        YKSearchDrugItem *item = [YKSearchDrugItem itemWithModel:model];
        item.selectionHandler = ^(__kindof YKSearchDrugItem *item, NSIndexPath *indexPath) {
            if (self.selectDrugComplete) {
                self.selectDrugComplete(item.model);
            }
        };
        [section addItem:item];
    }
    [self.tableViewManager addSection:section];
    
    [self.tableView reloadData];
}



@end
